from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from routes import router
from sales_prediction import load_and_process_data


@asynccontextmanager
async def lifespan(app: FastAPI):
    yield

    # 关闭时的操作
    print("应用关闭，正在清理...")
    load_and_process_data.cache_clear()
    print("缓存已清理")


app = FastAPI(lifespan=lifespan)
app.include_router(router)
app.mount("/static", StaticFiles(directory="static"), name="static")


if __name__ == "__main__":
    import uvicorn

    #  uvicorn main:app --reload --port 8003#,原来7009
    uvicorn.run("main:app", host="0.0.0.0", port=7009, workers=1, reload=True)
